﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ConfApp.Infrastructure;
using ConfApp.Models;
using Microsoft.Phone.Reactive;

namespace ConfApp.Services.Twitter
{
    public class ConfDataUpdateService : IConfDataUpdateService
    {
        public const string SynchronizingTask = "SynchronizingTask";

        private IConfDataServiceClient _confDataServiceClient;
        private IConfDataStoreLocator _confDataStoreLocator;
        private ISettingsStore _settingsStore;

        public ConfDataUpdateService(
            IConfDataServiceClient confDataServiceClient,
            IConfDataStoreLocator confDataStoreLocator,
            ISettingsStore settingsStore)
        {   
            _confDataServiceClient = confDataServiceClient;
            _confDataStoreLocator = confDataStoreLocator;
            _settingsStore = settingsStore;
        }

        public IObservable<TaskCompletedSummary> StartUpdating()
        {
            var store = _confDataStoreLocator.GetStore();
            var phoneMpsnUrl = _settingsStore.PhoneMpnsUrl;
            if(string.IsNullOrEmpty(phoneMpsnUrl))
            {
                
            }
            return _confDataServiceClient.GetData(
                store.Version,phoneMpsnUrl,store.GetSpeakers())
                .Select(confData =>
                            {
                                var result = store.SaveConfData(confData);

                                return new TaskCompletedSummary
                                {
                                    Task = SynchronizingTask,
                                    Result = TaskSummaryResult.Success,
                                    Context = result.ToString()
                                };
                            })
                            .Catch(
                        (Exception exception) =>
                        {
                            if (exception is WebException)
                            {
                                var webException = exception as WebException;
                                var summary = ExceptionHandling.GetSummaryFromWebException(SynchronizingTask, webException);
                                return Observable.Return(summary);
                            }

                            if (exception is UnauthorizedAccessException)
                            {
                                return Observable.Return(new TaskCompletedSummary { Task = SynchronizingTask, Result = TaskSummaryResult.AccessDenied });
                            }

                           ;
                            return Observable.Return(
                                new TaskCompletedSummary { Task = SynchronizingTask, Result = TaskSummaryResult.UnknownError }
                                );
                        });
        }
    }
}
